package com.flashphoner.click2call
{
	import com.flashphoner.api.Flash_API;
	import com.flashphoner.api.data.Call;
	import com.flashphoner.api.data.ErrorCodes;
	import com.flashphoner.api.data.PhoneConfig;
	import com.flashphoner.api.interfaces.APINotify;
	
	import flash.external.ExternalInterface;
	
	import mx.controls.Alert;
	
	public class APINotifyClick2Call implements APINotify
	{
		public function APINotifyClick2Call()
		{
		}
		public function notifyCloseConnection():void{
			DataClick2Call.getInstance().phoneButton.toCall();
		}
		public function notifyConnected():void{
				if (!PhoneConfig.REGISTER_REQUIRED){				
					var callee:String = String(ExternalInterface.call("getElementValue","callee"));					
					Flash_API.getInstance().call(callee,Flash_API.getInstance().modelLocator.visibleName);
				}			
		}
		public function notifyRegistered(_sipObject:Object):void{
			if (PhoneConfig.REGISTER_REQUIRED){	
				var callee:String = String(ExternalInterface.call("getElementValue","callee"));							
				Flash_API.getInstance().call(callee,Flash_API.getInstance().modelLocator.visibleName);
			}			
		}
		public function notifyBalance(balance:Number,_sipObject:Object):void{
			
		}
		public function notify(state:String,_sipObject:Object):void{
			if (state == Call.STATE_BUSY || state == Call.STATE_FINISH){
				DataClick2Call.getInstance().phoneButton.toCall();
				Flash_API.getInstance().logoff();
			}
			DataClick2Call.getInstance().stateCall = state;
		}
		public function notifyCost(cost:Number):void{
			
		}
		public function notifyCall(caller:String,visibleNameCaller:String,_sipObject:Object):void{
			Flash_API.getInstance().hangup();
		}
		public function notifyError(error:String,_sipObject:Object = null):void{
			if (error == ErrorCodes.CONNECTION_ERROR){				
				DataClick2Call.getInstance().viewController.failConnectionView();				
			} else
			if (error == ErrorCodes.AUTHENTICATION_FAIL){				
				DataClick2Call.getInstance().viewController.failRegisterView();
			} else
			if (error == ErrorCodes.USER_NOT_AVAILABLE){
				DataClick2Call.getInstance().viewController.calleeNotFoundView();	
			} else if (error==ErrorCodes.TOO_MANY_REGISTER_ATTEMPTS){
				mx.controls.Alert.show("Connection error");
				DataClick2Call.getInstance().phoneButton.enabled = false;
			} else if (error==ErrorCodes.LICENSE_RESTRICTION){
				mx.controls.Alert.show("License restriction: 'Trial period has been expired' OR 'Too many connects'. Check the serial number.");
			} else if (error==ErrorCodes.INTERNAL_SIP_ERROR){
				mx.controls.Alert.show("There is a SIP problem in Flashphoner side or your SIP-provider side. Please send logs and other info to Flashphoner support team.");
			} else 
			if (error == ErrorCodes.REGISTER_EXPIRE){
				DataClick2Call.getInstance().viewController.registerAttemptExpire();	
			} else if (error == ErrorCodes.SIP_PORTS_BUSY){
				DataClick2Call.getInstance().viewController.sipPortsBusyView();	
			} else if (error == ErrorCodes.MEDIA_PORTS_BUSY){
				DataClick2Call.getInstance().viewController.mediaPortsBusyView();	
			} else if (error == ErrorCodes.WRONG_SIPPROVIDER_ADDRESS){
				DataClick2Call.getInstance().viewController.wrongSipProviderAddressView();	
			}			
			DataClick2Call.getInstance().phoneButton.toCall();
			Flash_API.getInstance().logoff();
		} 

	}
}